package cn.iocoder.yudao.module.famous.doctor.controller.app.inquiry;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.inquiry.vo.*;
import cn.iocoder.yudao.module.famous.doctor.convert.inquiry.InquiryConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.inquiry.InquiryDO;
import cn.iocoder.yudao.module.famous.doctor.service.inquiry.InquiryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 我的问诊")
@RestController
@RequestMapping("/famous.doctor/inquiry/app")
@Validated
public class AppInquiryController {

    @Resource
    private InquiryService inquiryService;

    @PostMapping("/create")
    @Operation(summary = "创建我的问诊")
    public CommonResult<Long> createInquiry(@Valid @RequestBody InquiryCreateReqVO createReqVO) {
        return success(inquiryService.createInquiry(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新我的问诊")
    public CommonResult<Boolean> updateInquiry(@Valid @RequestBody InquiryUpdateReqVO updateReqVO) {
        inquiryService.updateInquiry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除我的问诊")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteInquiry(@RequestParam("id") Long id) {
        inquiryService.deleteInquiry(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得我的问诊")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<InquiryRespVO> getInquiry(@RequestParam("id") Long id) {
        InquiryDO inquiry = inquiryService.getInquiry(id);
        return success(InquiryConvert.INSTANCE.convert(inquiry));
    }

    @PostMapping("/getDiagnoseLabel")
    @Operation(summary = "复诊获取初诊信息")
    public CommonResult<InquiryRespVO> getDiagnoseLabel(@Valid @RequestBody InquiryReqLabelVO exportReqVO) {
        InquiryDO inquiry = inquiryService.getDiagnoseLabel(exportReqVO);
        if (ObjectUtil.isEmpty(inquiry)){
            return error(999999,"该诊断人没有初诊记录");
        }
        return success(InquiryConvert.INSTANCE.convert(inquiry));
    }

    @GetMapping("/list")
    @Operation(summary = "获得我的问诊列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<InquiryRespVO>> getInquiryList(@RequestParam("ids") Collection<Long> ids) {
        List<InquiryDO> list = inquiryService.getInquiryList(ids);
        return success(InquiryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得我的问诊分页")
    public CommonResult<PageResult<InquiryRespVO>> getInquiryPage(@Valid InquiryPageReqVO pageVO) {
        PageResult<InquiryDO> pageResult = inquiryService.getInquiryPage(pageVO);
        return success(InquiryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getIoutpatientList")
    @Operation(summary = "获得就诊列表分页")
    public CommonResult<PageResult<InquiryRespVO>> getIoutpatientList(@Valid InquiryPageReqVO pageVO) {
        PageResult<InquiryDO> pageResult = inquiryService.getIoutpatientList(pageVO);
        return success(InquiryConvert.INSTANCE.convertPage(pageResult));
    }
}
